/**
********************************************************
*
* @file      delay.c
* @author    Gizwtis
* @version   V2.3
* @date      2015-07-06
*
* @brief     ???.????????
*            Gizwits Smart Cloud  for Smart Products
*            ??|???|??|??|??|??|??|??
*            www.gizwits.com
*
*********************************************************/

#include "delay.h"

static uint8_t  fac_us=0;																		//us?????
static uint16_t fac_ms=0;																		//ms?????

void Delay_Init(uint8_t SYSCLK)
{
	SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);	//??????,HCLK/8
	fac_us=SYSCLK/8; //????,fac_us???????????????
	fac_ms =(u16)fac_us*1000; 	
}			    								   
void Delay_us(uint32_t nus)
{		
		uint32_t temp;  
		SysTick->LOAD = nus*fac_us;  //??10us,10*9 = 90,??load????
		SysTick->VAL=0x00;//????0
		SysTick->CTRL = 0x01;//??????,?????????0???????
		do  
		{  
			 temp = SysTick->CTRL;//???,???????1,????????0
		}  
		while(temp & 0x01 && !(temp &(1<<16)));//??
		SysTick->CTRL = 0x00;//?????
		SysTick->VAL = 0x00;//??val,?????
}


void Delay_ms(uint16_t nms)
{	 		  	  
		uint32_t temp;  
		SysTick->LOAD = nms*fac_ms;//??10ms,10*9 = 90,??load????
		SysTick->VAL=0x00;//????0
		SysTick->CTRL = 0x01;//??????,?????????0???????
		do  
		{  
			 temp = SysTick->CTRL;//???,???????1,????????0
		}  
		while(temp & 0x01 && !(temp &(1<<16)));//??
	
		SysTick->CTRL = 0x00;//?????
		SysTick->VAL = 0x00;//??val,?????
} 
